package com.yami.shop.config;


import cn.hutool.core.util.StrUtil;
import com.yami.shop.common.bean.*;
import com.yami.shop.common.config.Constant;
import com.yami.shop.common.constants.PaypalConstant;
import com.yami.shop.common.exception.YamiShopBindException;
import com.yami.shop.service.SysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Objects;

/**
 * 商城配置文件
 * 支付配置、文件上传配置、短信配置、快递配置、小程序配置、微信网页开发配置、公众号配置
 * @author yami
 */
@Component
public class ShopConfig {

    @Autowired
    private SysConfigService sysConfigService;

    public Qiniu getQiniu(){
        // 从数据库 / 缓存中获取到配置文件信息
        return sysConfigService.getSysConfigObject(Constant.QINIU_CONFIG, Qiniu.class);
    }

    public AliOss getAliOss() {
        return sysConfigService.getSysConfigObject(Constant.ALI_OSS_CONFIG, AliOss.class);
    }

    public QCloud getQCloud() {
        return sysConfigService.getSysConfigObject(Constant.Q_CLOUD_CONFIG,QCloud.class);
    }

    public HuaWeiOss getHuaWeiObs() {
        return sysConfigService.getSysConfigObject(Constant.HUAWEI_OBS_CONFIG, HuaWeiOss.class);
    }

    public Minio getMinio() {
        return sysConfigService.getSysConfigObject(Constant.MINIO_OSS_CONFIG, Minio.class);
    }

    public ALiDaYu getAliDaYu(){
        ALiDaYu aLiDaYu = sysConfigService.getSysConfigObject(Constant.ALIDAYU_CONFIG, ALiDaYu.class);
        if (aLiDaYu == null || StrUtil.isBlank(aLiDaYu.getAccessKeyId())) {
            // 无法获取短信配置，无法发送短信
            throw new YamiShopBindException("yami.sys.sms.no.exist");
        }
        return aLiDaYu;
    }


    public QuickBird getQuickBird() {
        return sysConfigService.getSysConfigObject(Constant.QUICKBIRD_CONFIG, QuickBird.class);
    }

    public Quick100 getQuick100() {
        return sysConfigService.getSysConfigObject(Constant.QUICK100_CONFIG, Quick100.class);
    }

    public QuickAli getAliQuick() {
        return sysConfigService.getSysConfigObject(Constant.ALI_QUICK_CONFIG, QuickAli.class);
    }


    public WxMiniApp getWxMiniApp() {
        WxMiniApp wxMiniApp = sysConfigService.getSysConfigObject(Constant.MA_CONFIG, WxMiniApp.class);
        if (wxMiniApp == null || StrUtil.isBlank(wxMiniApp.getAppId())) {
            // 请在后台页面设置微信小程序信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxMa.no.exist");
        }
        return wxMiniApp;
    }

    public WxMp getWxMp() {
        WxMp wxMp = sysConfigService.getSysConfigObject(Constant.MP_CONFIG, WxMp.class);
        if (wxMp == null || StrUtil.isBlank(wxMp.getAppId())) {
            // 请在后台页面设置微信公众号信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxMp.no.exist");
        }
        return wxMp;
    }

    public WxApp getWxApp() {
        WxApp wxApp = sysConfigService.getSysConfigObject(Constant.MX_APP_CONFIG, WxApp.class);
        if (wxApp == null || StrUtil.isBlank(wxApp.getAppId())) {
            // 请在后台页面设置微信开放平申请应用相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxApp.no.exist");
        }
        return wxApp;
    }

    public WxPay getWxPay() {
        WxPay wxPay = sysConfigService.getSysConfigObject(Constant.WXPAY_CONFIG, WxPay.class);
        if (wxPay == null || StrUtil.isBlank(wxPay.getMchId())) {
            // 请在后台页面设置微信支付信息后，再进行操作
            throw new YamiShopBindException("yami.sys.wxPay.no.exist");
        }
        return wxPay;
    }

    public Alipay getAlipay() {

        return sysConfigService.getSysConfigObject(Constant.ALIPAY_CONFIG,Alipay.class);
    }
    public UnionPay getUnionPay() {
        UnionPay unionPay = sysConfigService.getSysConfigObject(Constant.UNIONPAY_CONFIG, UnionPay.class);
        if (unionPay == null || StrUtil.isBlank(unionPay.getMachId())) {
            // 请在后台页面设置微信支付信息后，再进行操作
            throw new YamiShopBindException("yami.sys.unionPay.no.exist");
        }
        return unionPay;
    }

    public HnaPay getHnaPay() {
        HnaPay hnaPay = sysConfigService.getSysConfigObject(Constant.HNAPAY_CONFIG, HnaPay.class);
        if (hnaPay == null || StrUtil.isBlank(hnaPay.getMerId())) {
            // 请在后台页面设置微信支付信息后，再进行操作
            throw new YamiShopBindException("yami.sys.hnaPay.no.exist");
        }
        return hnaPay;
    }

    public Domain getDomain() {
        Domain domain = sysConfigService.getSysConfigObject(Constant.DOMAIN_CONFIG, Domain.class);
        if (domain == null || StrUtil.isBlank(domain.getApiDomainName())) {
            // 请在后台页面设置回调域名后，再进行操作
            throw new YamiShopBindException("yami.sys.wxDomain.no.exist");
        }
        return domain;
    }

    public PayPal getPayPal() {
        PayPal payPal = sysConfigService.getSysConfigObject(Constant.PAYPAL_CONFIG, PayPal.class);
        boolean is = Objects.isNull(payPal)
                || StrUtil.isBlank(payPal.getClientId())
                || StrUtil.isBlank(payPal.getClientSecret());
        if (is) {
            // 请在后台页面设置paypal相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.paypal.no.exist");
        }
        if (StrUtil.isBlank(payPal.getMode())) {
            payPal.setMode(PaypalConstant.PAYPAL_MODE);
        }
        return payPal;
    }

    public ExchangeRate getExchangeRate() {
        ExchangeRate exchangeRate = sysConfigService.getSysConfigObject(Constant.EXCHANGE_RATE_CONFIG, ExchangeRate.class);
        if (exchangeRate == null) {
            // 请在后台页面设置paypal相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.paypal.no.exist");
        }
        return exchangeRate;
    }

    public LangConfig getLang() {
        LangConfig langConfig = sysConfigService.getSysConfigObject(Constant.INTERNATIONALIZATION_CONFIG, LangConfig.class);
        return langConfig;
    }
    private String getEbusinessIdByStr(String paramValue){
        String[] split = paramValue.split(",");
        for (String s : split) {
            if (s.contains("eBusinessID")){
                String[] split1 = s.split(":");
                String str = split1[1];
                String replace = str.replace("\"", "");
                return replace;
            }
        }
        return "";
    }
    //访问apigateway中间转发接口配置
    public HSExchange getAPIExchange() {
        HSExchange sysConfigObject = sysConfigService.getSysConfigObject(Constant.API_GATEWAY_CONFIG, HSExchange.class);
        if (sysConfigObject == null || !sysConfigObject.getIsOpen()) {
            // 请在后台页面设置paypal相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.apigateway.no.exist");
        }
        return sysConfigObject;
    }
    //转赠年龄限制
    public String getNftThansferAge() {
        String ageStr = sysConfigService.getSysConfigObject(Constant.NFT_THANSFER_AGE, String.class);
        if (ageStr == null) {
            throw new YamiShopBindException("yami.personnft.thansfer.age.check");
        }
        return ageStr;
    }

    //划转上限
    public String getTransfeTMaxCount() {
        String ageStr = sysConfigService.getSysConfigObject(Constant.TRANSFET_MAX_COUNT, String.class);
        if (ageStr == null) {
            throw new YamiShopBindException("yami.personnft.thansfer.maxCount.check");
        }
        return ageStr;
    }
    //划转上限
    public String getVerifyProdid() {
        String prodidStr = sysConfigService.getSysConfigObject(Constant.VERIFY_PRODID_URL, String.class);
        if (prodidStr == null) {
            throw new YamiShopBindException("yami.personnft.thansfer.maxCount.check");
        }
        return prodidStr;
    }

    //经纪商
    public String getHwjServicePhone() {
        String phone = sysConfigService.getSysConfigObject(Constant.HWJSERVICE_PHONE_CONFIG, String.class);
        if (phone == null) {
            throw new YamiShopBindException("yami.personnft.thansfer.servicephone.check");
        }
        return phone;
    }

    //访问nft 授权接口
    public NftGrant getNftGrant(){
        NftGrant sysConfigObject = sysConfigService.getSysConfigObject(Constant.NFT_GRANT_CONFIG, NftGrant.class);
        if (sysConfigObject == null) {
            // 请在后台页面设置nft相关信息后，再进行操作
            throw new YamiShopBindException("yami.sys.nft.no.exist");
        }
        return sysConfigObject;
    }

    //配置nft 铸币助计词
    public NftMnemonic getNftMnemonic(){
        NftMnemonic sysConfigObject = sysConfigService.getSysConfigObject(Constant.NFT_MNEMONIC_CONFIG, NftMnemonic.class);
        if (sysConfigObject == null) {
            // 请在后台页面设置nft相关信息后，再进行操作
            throw new YamiShopBindException("yami.nft.mnemonic.no.exist");
        }
        return sysConfigObject;
    }
    //中间账户助记词和地址
    public MidMnemonic getMidMnemonic() {
        MidMnemonic sysConfigObject = sysConfigService.getSysConfigObject(Constant.NFT_MID_MNEMONIC, MidMnemonic.class);
        if (sysConfigObject == null) {
            throw new YamiShopBindException("yami.nft.mnemonic.no.exist");
        }
        return sysConfigObject;
    }
    public boolean isOpenSms(String smsTypeConfig) {
        SwitchBaseModel sysConfigObject = sysConfigService.getSysConfigObject(smsTypeConfig, SwitchBaseModel.class);
        return sysConfigObject.getIsOpen();
    }

    public TencentSMS getTencentSms() {
        return sysConfigService.getSysConfigObject(Constant.TENCENTSMS_CONFIG,TencentSMS.class);
    }


    public String getSmsWhitelist() {
        return sysConfigService.getSysConfigObject(Constant.SMS_WHITELIST, String.class);
    }

    /**
     * 产权登记配置
     * @return
     */
    public Works getWorksConfig() {
        return sysConfigService.getSysConfigObject(Constant.WORKS_CONFIG, Works.class);
    }

    public ZteFile getZteFileUpload() {
        return sysConfigService.getSysConfigObject(Constant.ZTE_UPLOAD, ZteFile.class);
    }
    //获取url
    public String getApiGatewayUrl() {
        String ageStr = sysConfigService.getSysConfigObject(Constant.APIGATEWAY_URL, String.class);
        if (ageStr == null) {
            throw new YamiShopBindException("yami.sys.hengsheng.no.exist");
        }
        return ageStr;
    }
    public String getBigMan() {
        return sysConfigService.getSysConfigObject(Constant.BIGMAN_CONFIG, String.class);
    }

    public String getDeliveryExpireMaxDay() {
        return sysConfigService.getSysConfigObject(Constant.DELIVERY_MAX_DAY_CONFIG, String.class);
    }

    public String getMall4JChannel() {
        return sysConfigService.getSysConfigObject(Constant.MALL4J_CHANNEL_CODE, String.class);
    }

}
